//
//  ChangePwViewController.m
//  shijianpinggu
//
//  Created by apple on 2017/11/6.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ChangePwViewController.h"

@interface ChangePwViewController ()
@property (weak, nonatomic) IBOutlet UITextField *oldPwText;
@property (weak, nonatomic) IBOutlet UITextField *resetPwText;
@property (weak, nonatomic) IBOutlet UITextField *resetPwDo;
@property (weak, nonatomic) IBOutlet UIButton *finshButton;
- (IBAction)finshButtonAction:(UIButton *)sender;

@end

@implementation ChangePwViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    
    self.finshButton.layer.cornerRadius = 4;
    self.finshButton.layer.masksToBounds = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)finshButtonAction:(UIButton *)sender {
    if (![self.resetPwText.text isEqualToString:self.resetPwDo.text]) {
        [self alertWithString:@"请确认两次输入密码是否一致" Code:@"0"];
    }else{
        [self changePwUpto];
    }
}
//请求数据
-(void)changePwUpto{
    
    
    NSDictionary *parameter = @{@"contactid":[[NSUserDefaults standardUserDefaults] stringForKey:@"userid"],@"jiupassword":self.oldPwText.text,@"xinpassword":self.resetPwText.text};
    
    //    NSLog(@"%@",dict);
    
    // 初始化Manager
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 不加上这句话，会报“Request failed: unacceptable content-type: text/plain”错误，因为我们要获取text/plain类型数据
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    // post请求
    [manager POST:@"http://wangan.ldynet.cn/index.php/IndexAppSet/edit_password" parameters:parameter constructingBodyWithBlock:^(id  _Nonnull formData) {
        // 拼接data到请求体，这个block的参数是遵守AFMultipartFormData协议的。
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        // 这里可以获取到目前的数据请求的进度
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        // 请求成功，解析数据
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSLog(@"11111111111111%@", dic);
        
        if ([dic[@"code"] integerValue] == 200) {
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
            [self alertWithString:dic[@"message"] Code:dic[@"code"]];
            
        }else{
            [self alertWithString:dic[@"message"] Code:dic[@"code"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 请求失败
        // [self alertWithString:@"请稍后再试"];
    }];
    
    
    //[self loginUrl];
}
-(void)alertWithString:(NSString *)string Code:(NSString *)code{
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if ([code integerValue] == 200) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    }];
    [alertC addAction:otherAction];
    [self.navigationController showDetailViewController:alertC sender:nil];
    
}

@end
